package com.pinyougou.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public final class MyUtil {

    private MyUtil(){}

    /**
     * 统计给定文件中给定字符串的出现次数
     * @param fileName
     * @param str
     * @return
     */
    public static int countStrInFile(String fileName,String str){
        int count = 0;
        try {
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            String line = br.readLine();
            while (line != null){
                int index = line.indexOf(str);
                while (line.length() >= str.length() && index >=0){
                    count++;
                    line = line.substring(index + str.length());
                }
            }
        } catch (IOException e) {
            System.out.println("操作异常");
        }
        return count;
    }

}
